import 'package:flutter/material.dart';

import 'AnimationImage.dart';

/// @date 2020/08/06  17:11
/// @author 程志强
/// @des 基础缩放动画

class AnimationScaleRoute3 extends StatefulWidget {
  @override
  _AnimationScaleRoute3State createState() => _AnimationScaleRoute3State();
}

class _AnimationScaleRoute3State extends State<AnimationScaleRoute3>
    with SingleTickerProviderStateMixin {
  Animation<double> _animation;
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(duration: const Duration(seconds: 1), vsync: this);
    // 使用弹性曲线
    _animation = CurvedAnimation(parent: _controller, curve: Curves.bounceIn);
    // 图片宽高从0变到300
    _animation = Tween(begin: 0.0, end: 300.0).animate(_controller);
    _animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        // 动画结束时反向执行动画
        _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        // 动画恢复到初始状态时执行动画（正向）
        _controller.forward();
      }
    });

    // 启动动画(正向)
    _controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("基础动画3_AnimatedBuilder"),
      ),
      /*body: AnimatedBuilder(
          animation: _animation,
          child: Image.network(
              "https://inews.gtimg.com/newsapp_bt/0/10735498766/1000"),
          builder: (context, child) {
            return Center(
              child: Container(
                width: _animation.value,
                height: _animation.value,
                child: child,
              ),
            );
          },
        )*/

      body: GrowTransition(
        child: Image.network(
            "https://inews.gtimg.com/newsapp_bt/0/10735498766/1000"),
        animation: _animation,
      ),
    );
  }

  @override
  void dispose() {
    // 路由销毁时释放动画资源
    _controller.dispose();
    super.dispose();
  }
}
